package com.hongchen.wechat.config;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.config.*;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.jfplugin.mail.MailPlugin;
import com.realdtech.admin.common.model._SysMappingKit;
import com.realdtech.base.UUIDHex;
import com.realdtech.base.jfinal.DruidConfig;
import com.realdtech.base.jfinal.directive.Compressor;
import com.realdtech.base.jfinal.log.Slf4jLogFactory;
import com.realdtech.base.shiro.ShiroMethod;
import com.realdtech.base.shiro.ShiroPlugin;
import com.realdtech.base.sms.SmsSenderPlugin;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.snaker.jfinal.plugin.SnakerPlugin;

/**
 * 系统配置类
 *
 * @author kris
 */
public class XWSConfig extends JFinalConfig {
    /**
     * 在configRoute中进行赋值，ShiroPlugin中需要用到
     */
    private Routes routes;


    /**
     * 配置系统常量
     */
    @Override
    public void configConstant(Constants me) {
        loadPropertyFile("cfg.txt");
//        me.setLogFactory(new Log4jLogFactory());
        me.setLogFactory(new Slf4jLogFactory());
        me.setBaseDownloadPath(getProperty("application.download_path"));
        me.setBaseUploadPath(getProperty("application.upload_path"));
        me.setDevMode(getPropertyToBoolean("application.dev_mode", false));

        // 错误页面
        //me.setError404View("/WEB-INF/template/dxd/404.html");
        //me.setError500View("/WEB-INF/template/dxd/500.html");
    }

    /**
     * 配置系统路由
     */
    @Override
    public void configRoute(Routes me) {
        //me.add(new AdminRoutes());
        me.add(new FrontRoutes());

        routes = me;
    }

    /**
     * 配置模板引擎
     */
    @Override
    public void configEngine(Engine me) {
        // me.setBaseTemplatePath("template");
        me.addDirective("compressor", new Compressor());
        me.addSharedObject("shiro", new ShiroMethod());
        me.addSharedObject("StringUtils", new StringUtils());
        //Sme.addSharedObject("loanProdUtils", new LoanProdUtils());
        me.addSharedObject("UUIDHex", new UUIDHex());
        me.addSharedObject("DateFormatUtils", new DateFormatUtils());

        me.setDevMode(JFinal.me().getConstants().getDevMode());

    }

    /**
     * 配置系统插件
     */
    @Override
    public void configPlugin(Plugins me) {
        /**
         * JDBC
         */
        DruidPlugin druid = DruidConfig.config(PropKit.use("jdbc.txt"));
        //配置SQL监控
        druid.addFilter(new StatFilter());
        WallFilter wall = new WallFilter();
        wall.setDbType("mysql");
        druid.addFilter(wall);
        me.add(druid);

        ActiveRecordPlugin fcArp = new ActiveRecordPlugin("fc", druid);
        fcArp.setShowSql(JFinal.me().getConstants().getDevMode());
        _SysMappingKit.mapping(fcArp);
        com.hongchen.model._MappingKit.mapping(fcArp);

        me.add(fcArp);

        /*ActivitiPlugin activitiPlugin = new ActivitiPlugin();
        me.add(activitiPlugin);*/
        /**
         * ShiroPlugi
         */
        ShiroPlugin shiroPlugin = new ShiroPlugin(routes);
        me.add(shiroPlugin);
        /**
         * QuartzPlugin
         */
        // QuartzPlugin quartzPlugin = new QuartzPlugin("job.txt",
        // "quartz.properties");
        // me.add(quartzPlugin);
        /**
         * RedisPlugin
         */
        // RedisPlugin redisPlugin = createRedisPlugin();
        // redisPlugin.setSerializer(new JdkSerializer());
        // me.add(redisPlugin);
        /**
         * EhCachePlugin
         */
        me.add(new EhCachePlugin());
        /**
         * OSSPlugin
         */
//        OSSPlugin realdUpload = new OSSPlugin("oss-reald-upload.txt");
//        me.add(realdUpload);
        /**
         * Cron4jPlugin
         */

        //Cron4jPlugin cp = new Cron4jPlugin("cron4j.txt");
        //me.add(cp);


        //EventPlugin eventPlugin = new EventPlugin();
        //
        // 开启异步
//        eventPlugin.async();
//        eventPlugin.scanPackage("com.realdtech.pb");
//        me.add(eventPlugin);

        me.add(new SmsSenderPlugin());
        /**添加邮件插件*/
        me.add(new MailPlugin(PropKit.use("mail.properties").getProperties()));

        SnakerPlugin snakerPlugin = new SnakerPlugin(druid, PropKit.use("snaker.properties").getProperties());
        me.add(snakerPlugin);


    }

    /**
     * 配置全局拦截器
     */
    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new Interceptor() {
            @Override
            public void intercept(Invocation invocation) {
                Controller c = invocation.getController();
                //
                c.setAttr("__devMode", JFinal.me().getConstants().getDevMode());
                invocation.invoke();
            }
        });
    }

    /**
     * 配置处理器
     */
    @Override
    public void configHandler(Handlers me) {
        DruidStatViewHandler dvh = new DruidStatViewHandler("/druid");
        me.add(dvh);

    }

    /*
     * (non-Javadoc)
     *
     * @see com.jfinal.config.JFinalConfig#afterJFinalStart()
     */
    @Override
    public void afterJFinalStart() {
        // JsonRender.addExcludedAttrs(
        // LoginService.loginAccountCacheName,
        // LoginSessionInterceptor.remindKey,
        // FriendInterceptor.followNum, FriendInterceptor.fansNum,
        // FriendInterceptor.friendRelation
        // );
    }
    // private RedisPlugin createRedisPlugin() {
    // String password = getProperty("cache.redis.password");
    // if (StrKit.isBlank(password)) {
    // return new RedisPlugin("redis:shiro", getProperty("cache.redis.host"),
    // getPropertyToInt("cache.redis.port"),
    // getPropertyToInt("cache.redis.timeout"));
    // } else {
    // return new RedisPlugin("redis:shiro", getProperty("cache.redis.host"),
    // getPropertyToInt("cache.redis.port"),
    // getPropertyToInt("cache.redis.timeout"), password);
    // }
    // }
}
